/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

// TabContainer object

BM.TabContainer = function(element) {
    BM.TabContainer.initializeBase(this, [element]);
}

BM.TabContainer.prototype = {
    getFirstTab: function() {
        return this.getTabs()[0];
    },

    getTabs: function() {
        return $(this.get_element()).find("." + BM.Tab.Tab);
    },

    getTab: function(id) {
        return $(this.get_element()).find("#" + id)[0];
    },

    hasSelectedTab: function() {
        return $(this.get_element()).find(".Selected." + BM.Tab.Tab)[0];
    },

    getSelectedTab: function() {
        return $(this.get_element()).find(".Selected." + BM.Tab.Tab)[0];
    },

    hide: function() {
        $(this.get_element()).css("display", "none");
    },

    show: function() {
        $(this.get_element()).css("display", "");
    },

    getElementIds: function() {
        var tabs = this.getTabs();
        var ids = new Array();
        $(tabs).each(
            function() {
                ids[ids.length] = this.get_element().parentNode.id + "_" + this.id.substring(0, this.id.length - 4);
            }
        );
        return ids;
    },

    add: function(id, onclickText, content) {
        var tab = BM.TabContainer.createTab(this.get_element(), id, onclickText, content);
        this.get_element().appendChild(tab);
        return tab;
    },

    getSelectedElementId: function() {
        var selectedTab = this.getSelectedTab()
        if (!selectedTab) return;
        return selectedTab.id.substring(0, selectedTab.id.length - this.getSuffixLength());
    },

    getSuffixLength: function() {
        return 2 + BM.Tab.Tab.length;
    }
}

BM.TabContainer.registerClass("BM.TabContainer", AjaxControlToolkit.ControlBase);

BM.TabContainer.getFromElement = function(element) {
    var tc = $(element).find("." + BM.Tab.TabContainer)[0];
    if (tc) {
        return $find(tc.id);
    }
}

BM.TabContainer.createTab = function(parent, id, onclickText, content) {
    var tab = BM.Tab.createDiv(parent, BM.Tab.Tab);
    tab = parent.lastChild;
    tab.id = id + "__" + BM.Tab.Tab;
    if (onclickText != null) {
        tab.onclick = new Function(onclickText);
    }
    var tabLeft = BM.Tab.createDiv(tab, BM.Tab.TabLeft);
    var tabRight = BM.Tab.createDiv(tabLeft, BM.Tab.TabRight);
    tabRight.innerHTML += content;
    return tab;
}

// Tab object

BM.Tab = function(node) {
    BM.Tab.initializeBase(this, [node]);
    this.itemId;
    this.node = node;
    this.container = this.node.parentNode;
    this.containerObj = $find(this.container.id);
}

BM.Tab.TabLeft = "TbL";
BM.Tab.TabRight = "TbR";
BM.Tab.Tab = "Tb";
BM.Tab.TabContainer = "TC";
BM.Tab.Invisible = "Invisible";

BM.Tab.prototype = {
    // Cut off the suffix to get the item id
    getItemId: function() {
        if (this.itemId == null) {
            this.itemId = this.node.id.substring(0, this.node.id.lastIndexOf("__"));
        }
        return this.itemId;
    },

    dispose: function() {
        BM.Tab.callBaseMethod(this, 'dispose');
        if (this.node && this.node.parentNode) {
            this.node.parentNode.removeChild(this.node);
        }
    },

    getText: function() {
        return this.getTextNode().nodeValue;
    },

    setText: function(value) {
        this.getTextNode().nodeValue = value;
    },

    getTextNode: function() {
        return $(this.node).find('.' + BM.Tab.TabRight).contents().filter(function() { return this.nodeType == 3; })[0];
    },
    choose: function() {
        if (this.containerObj.hasSelectedTab()) {
            $($get(this.containerObj.getSelectedElementId())).addClass(BM.Tab.Invisible);
            $(this.containerObj.getSelectedTab()).removeClass("Selected");
        }
        $($get(this.getItemId())).removeClass(BM.Tab.Invisible);
        $(this.node).addClass("Selected");
    }
}

BM.Tab.get = function(node) {
    if (node.parentNode == null) return null;
    var tab = $find(node.id);
    if (tab == null) {
        tab = $create(BM.Tab, null, null, null, node);
    }
    return tab;
}

BM.Tab.choose = function(node) {    
    var tab = BM.Tab.get(node);
    if (tab == null) return null;
    return tab.choose();
}

BM.Tab.createDiv = function(parent, className) {
    var elem = document.createElement('div');
    if (parent != null) {
        parent.appendChild(elem);
    }
    if (className != null) {
        elem.className = className;
    }
    return elem;
}

BM.Tab.registerClass("BM.Tab", Sys.UI.Control);